<template>
  <div class="page"  >
    <ul class="calendar">
      <div
        style="width: 100%;
          display: flex;
          justify-content: space-between;
          align-items: center;
        "
      >
        <span
          style="font-size: 18px;cursor: pointer"
          @click="prevDayclick(1)"
        > 
        </span>
        <li class="header" style="justify-content: center" >
          {{
            currentYear +
            "年" +
            (currentMonth + 1) +
            "月"
          }}
        </li>
        <span
          style="font-size: 18px; cursor: pointer"
          @click="nextDayclick(1)"
          >></span
        >
      </div>
      <li class="week" >
        <p >日</p>
        <p>一</p>
        <p>二</p>
        <p>三</p>
        <p>四</p>
        <p>五</p>
        <p>六</p>
      </li>
      <li class="row day">
        <span
          @click="prevDayclick(item)"
          class="date prevDay"
          v-for="item in prevDays"
          :key="'A' + item"
          >{{ item }}</span
        >
        <span
          @click="clickdate(item)"
          v-for="item in currentDays"
          :key="'B' + item"
          class="date nowm"
          :class="{
            jintian: currentDay == item
          }"
          >{{ item }}</span
        >
        <span
          @click="nextDayclick(item)"
          class="date prevDay"
          v-for="item in nextDays"
          :key="'A' + item"
          >{{ item }}</span
        >
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data () {
    return {
      week: ['日', '一', '二', '三', '四', '五', '六'],
      currentDay: new Date().getDate(),
      currentDay2: new Date().getDate(),
      currentMonth: new Date().getMonth(),
      currentYear: new Date().getFullYear()
    }
  },
  watch: {
    currentMonth (newv, oldv) {
      if (newv === new Date().getMonth()) {
        document.getElementsByClassName('nowm')[this.currentDay2 - 1].style.color = '#3F62AE'
      }
    }
  },
  computed: {
    currentMonthChinese () {
      return new Date(this.currentYear, this.currentMonth).toLocaleString(
        'default',
        { month: 'short' }
      )
    },
    currentDays () {
      return new Date(this.currentYear, this.currentMonth + 1, 0).getDate()
    },
    prevDays () {
      let data = new Date(this.currentYear, this.currentMonth, 0).getDate()
      let num = new Date(this.currentYear, this.currentMonth, 1).getDay()
      const days = []
      while (num > 0) {
        days.push(data--)
        num--
      }
      return days.sort()
    },
    nextDays () {
      const m = this.currentMonth + 1
      let num = new Date(this.currentYear, m, 1).getDay()
      const days = []
      let number = 0
      while (num < 7) {
        number++
        days.push(number)
        num++
      }
      return days.sort()
    },
    zongdate () {
      return this.currentYear + '-' + (this.currentMonth + 1) + '-' + this.currentDay
    }
  },
  methods: {
    change () {
      this.currentMonth = Number(this.inputVal) - 1
    },
    clickdate (val) {
      this.currentDay = val
      if (this.currentMonth === new Date().getMonth() && this.currentDay !== new Date().getDate()) {
        document.getElementsByClassName('nowm')[this.currentDay2 - 1].style.color = '#3F62AE'
      } 
    },
    prevDayclick (val) {
      this.currentDay = val
      this.currentMonth = Number(this.currentMonth) - 1
      document.getElementsByClassName('nowm')[this.currentDay2 - 1].style.color = 'black'
      if (this.currentMonth < 0) {
        this.currentMonth = 11
        this.currentYear = Number(this.currentYear - 1)
      } 
    },
    nextDayclick (val) {
      this.currentDay = val
      this.currentMonth = Number(this.currentMonth) + 1
      document.getElementsByClassName('nowm')[this.currentDay2 - 1].style.color = 'black'
      if (this.currentMonth > 11) {
        this.currentMonth = 0
        this.currentYear = Number(this.currentYear + 1)
      } 
    }
  }
}
</script>

<style lang="scss">
.calendar {
  width: 300px;
  .header {
    display: flex;
    align-items: center; 
    border-radius: 4px 4px 0px 0px;
    font-size: 18px;
    font-family: PingFangSC-Semibold, PingFang SC;
    font-weight: 400;
    color: #404040;
    padding-left: 20px;
  }
  .week {
    width: 100%;
    display: flex;
    justify-content: space-around; 
    background: #fefefe;
    font-size: 18px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #404040;
    p {
      width:14.2%; 
      text-align: center;
    }
  }
  .row {
    width: 100%;
    display: flex;
    justify-content: space-between;
  }
  .day {
    -webkit-box-pack: start;
    -ms-flex-pack: start;
    justify-content: flex-start;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    span { 
      text-align: center;
    }
    .date {
      display: flex;
      align-items: center;
      justify-content: center;
      width:14.2%;
      height:50px;
      font-size: 18px;
      font-family: PingFangSC-Medium, PingFang SC; 
      color: black;
    }
    .date:hover { 
      background-color: #3f62ae;
      color: black!important;;
      cursor: pointer;
    }
    .nowDay {
      background: #404040;
    }
  }
}
.jintian {  
  color: #3f62ae !important;
}
.prevDay{
  color: #ccc!important;
}
</style>

